#include "snake_select.h"
#include <QPainter>
#include <QPushButton>
#include "snake_hall.h"
#include <QSound>
#include <snake_room.h>
#include <QFile>
#include <QLabel>

Snake_select::Snake_select(QWidget *parent) : QWidget(parent)
{
    //设置标题和图标
    this->setWindowTitle("关卡选择");
    this->setWindowIcon(QIcon(":/res/35.png"));

    //添加返回按钮
    QPushButton* button_return=new QPushButton(this);
    //设置按钮图标
    button_return->setIcon(QIcon(":/res/exit.png"));
    QFont font1("华文行楷",15);
    button_return->setFont(font1);
    button_return->setText("返回主页面");
    button_return->move(900,750);

    //提前创建好下一个游戏界面窗口
    Snake_room* room=new Snake_room();

    //给返回按钮添加槽函数
    connect(button_return,&QPushButton::clicked,this,[=](){
          Snake_hall* hall=new Snake_hall();
          this->close();
          hall->setGeometry(this->geometry());
          hall->show();
          QSound::play("D:/qt_learn/Snake/res/history.txt");
          delete room;  //如果返回首页面，则释放我们创建的游戏窗口
    });


    //添加选择难度按钮和查看历史战绩按钮
    //简单模式
    QPushButton* easy=new QPushButton("简单模式",this);
    easy->move(470,150);
    QFont font("华文行楷",30);
    easy->setFont(font);
    connect(easy,&QPushButton::clicked,this,[=](){
       this->close();
       room->setGeometry(this->geometry());
       room->show();
       QSound::play(":/res/clicked.wav");
       room->moveSpeed(300);
    });
    //普通模式
    QPushButton* ordinary=new QPushButton("普通模式",this);
    ordinary->move(470,300);
    ordinary->setFont(font);
    connect(ordinary,&QPushButton::clicked,this,[=](){
       this->close();
       room->setGeometry(this->geometry());
       room->show();
       QSound::play(":/res/clicked.wav");
       room->moveSpeed(200);
    });
    //困难模式
    QPushButton* difficulty=new QPushButton("困难模式",this);
    difficulty->move(470,450);
    difficulty->setFont(font);
    connect(difficulty,&QPushButton::clicked,this,[=](){
       this->close();
       room->setGeometry(this->geometry());
       room->show();
       QSound::play(":/res/clicked.wav");
       room->moveSpeed(100);
    });
    //历史战绩
    QPushButton* history=new QPushButton("历史战绩",this);
    history->move(470,600);
    history->setFont(font);
    //历史战绩的窗口不一样，不需要创建snake_room窗口，而是创建历史战绩窗口
    //使用信号槽从文件中读取历史战绩
    connect(history,&QPushButton::clicked,this,[=](){
        QSound::play(":/res/clicked.wav");
        QWidget* widget=new QWidget();
        widget->setWindowTitle(QString("历史战绩"));
        widget->setWindowIcon(QIcon(":/res/35.png"));
        widget->setFixedSize(500,400);   //设置窗口基础大小

        //在窗口中加个text显示历史战绩
        QLabel* label=new QLabel(widget);
        label->setFont(font);

        //创建QFile对象，读取文件内容
        QFile file("D:/qt_learn/Snake/res/history.txt");
        file.open(QFile::ReadOnly);     //设置仅读
        int date=file.readLine().toUInt();  //从文件里读一行数据并转为数字
        label->setText(QString("历史最高战绩为：%1").arg(date));
        label->setWordWrap(true);   //设置自动换行
        file.close();
        widget->show();
    });

}

//重写绘制绘制背景
void Snake_select::paintEvent(QPaintEvent *event)
{
    (void)event;
    QPainter painter(this);

    QPixmap pixmap(":/res/game_select.png");

    painter.drawPixmap(0,0,this->width(),this->height(),pixmap);
}


